package com.nari.ysphd;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;

import com.jeremyfeinstein.okhttputil.OkHttpUtils;
import com.jeremyfeinstein.utils.ConferenceApp;
import com.jeremyfeinstein.utils.ToastUtil;
import com.nari.ysphd.util.ActivityManager;
import com.nari.ysphd.util.CommonUtil;
import com.sxc.loglib.LogConfig;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

public class MyApp extends ConferenceApp {

    private static final String TAG = MyApp.class.getSimpleName();
    public static boolean isLoginOut = true;
    private static final String LOG_APP_NAME = "ysphd_app";

    @Override
    public void onCreate() {
        super.onCreate();
        SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss", Locale.CHINA);
        String fileName = format.format(new Date(System.currentTimeMillis())) + ".log";
        LogConfig.getInstance()
                .setDebug(false)
                .isWriteLog(true)//是否在文件中记录，默认不记录
                .isSaveExternal(false) //默认保存在应用内
                .setFileName(LOG_APP_NAME + "_" + fileName)
                .setCrashName("crash_" + LOG_APP_NAME + "_" + fileName)
                .setFolderName(LOG_APP_NAME)
                .fileSize(100 * 1024)//日志文件的大小，默认0.1M,以bytes为单位
                .setUseShake(false)
                .setEmails("sunxunchao@gcsoft.com")
                .build(this)
                .start();

        try {
            OkHttpUtils.getInstance().setCertificates(getAssets().open("ca.crt"));
        } catch (IOException e) {
            e.printStackTrace();
        }
        this.registerActivityLifecycleCallbacks(new AppLifecycleCallback());
    }


    private class AppLifecycleCallback implements ActivityLifecycleCallbacks {
        @Override
        public void onActivityCreated(Activity activity, Bundle savedInstanceState) {
            ActivityManager.getInstance().pushActivity(activity);
        }

        @Override
        public void onActivityStarted(Activity activity) {

        }

        @Override
        public void onActivityResumed(Activity activity) {

        }

        @Override
        public void onActivityPaused(Activity activity) {

        }

        @Override
        public void onActivityStopped(Activity activity) {
            Log.d(TAG, "onActivityStopped: " + activity.getLocalClassName());
            Log.d(TAG, "onActivityStopped: 1 " + MyApp.isLoginOut);
            Log.d(TAG, "onActivityStopped: " + activity.getComponentName().getClassName());
            if (!MyApp.isLoginOut && CommonUtil.isBackground(MyApp.getApp())) {
                Log.d(TAG, "onActivityStopped: 2 " + MyApp.isLoginOut);
                ToastUtil.showLong(MyApp.getApp(), "音视频互动后台运行中");
            }
        }

        @Override
        public void onActivitySaveInstanceState(Activity activity, Bundle outState) {

        }

        @Override
        public void onActivityDestroyed(Activity activity) {
            ActivityManager.getInstance().popActivity(activity);
        }
    }
}
